//19 - 删除链表的倒数第n个节点
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(n <= 0 || head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(n-1 != 0){
            //这里只能走n-1步来找到倒数第n个节点，否则 [1,2],n=2 会返回空
            fast = fast.next;
            if(fast == null){
                return null;
            }
            n--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }

        if(slow == head){//头节点就是要删除的节点
            head = head.next;
            return head;
        }
        ListNode cur = head;//找到要删除节点的下一个节点
        while(cur.next != slow){
            cur = cur.next;
        }
        cur.next = slow.next;//跳过这一节点
        return head;
    }
}
public class Test {
}
